import { _decorator, Component, Node, Vec3, tween, v3, find } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('Coin')
export class Coin extends Component {
    public flyTo(targetNode: Node, onComplete: () => void) {
        if (!targetNode || !targetNode.active) {
            // 如果目标不存在或未激活，直接执行回调并销毁
            onComplete?.();
            this.node.destroy();
            return;
        }

        const targetPos = targetNode.worldPosition;
        const duration = 0.6 + Math.random() * 0.2;

        tween(this.node)
            .to(duration, { worldPosition: targetPos, scale: v3(0.3, 0.3, 0.3) }, { easing: 'quadIn' })
            .call(() => {
                onComplete?.();
                this.node.destroy();
            })
            .start();
    }
}
